require('date-utils');
var async = require('async');
var Q = require('q');
var AV = require('leanengine');

var config = require('../config');
var AE = require("apiengine")();


//操作产品的模型信息
var Card = AV.Object.extend("Card");
var Level = AV.Object.extend("Level");
var Order=AV.Object.extend("Order");

var User = AV.Object.extend('_User');
var AddressFlow = AV.Object.extend('UserAddress');
var CouponFlow = AV.Object.extend('UserCoupon');
var PointFlow = AV.Object.extend('PointFlow');
var AmountFlow = AV.Object.extend('AmountFlow');

module.exports = {

    /*
     * 下拉用户基本信息
     * */
    selectUser:function(){
        var promise = new AV.Promise();

        query = new AV.Query(User);
        query.notEqualTo('syncStatus',7);
        //query.greaterThan('amount',0);
        //query.equalTo('amount',0);
        query.descending("updatedAt");
        //query.ascending('updatedAt');
        query.limit(1000);
        query.find().then(function(obj){
            if(obj.length == 0){
                promise.resolve();
            }else{
                async.eachSeries(obj,function(each,cb){
                    each.set('syncStatus',7);
                    each.save().then(function(res){
                        var objectId = res.id;
                        var openid = res.get('openid');
                        var nickname = (res.get('nickname')||'');
                        var sex = (res.get('sex')||'');
                        var balance = (res.get('amount')||0);
                        var point = (res.get('point')||0);
                        var level = (res.get('level_id')||2);
                        var headimgurl = (res.get('headimgurl')||'');
                        var city = (res.get('city')||'');
                        var province = (res.get('province')||'');
                        var country = (res.get('country')||'');

                        var res1 = new AE.Object('gr_login_info');
                        res1.set({objectId:objectId,openid:openid,nickname:nickname,sex:sex,balance:balance,point:point,level:level,headimgurl:headimgurl,country:country,province:province,city:city});
                        res1.create().then(function(data){
                            cb(null)
                        }).catch(function(err){
                            cb(null)
                        });
                    }).catch(function(err){
                        cb(null)
                    });
                },function(){
                    promise.resolve(obj);
                })
            }
        }).catch(function(err,result){
            if(err){
                promise.reject(err);
            }else{
                promise.resolve(result);
            }
        });
        return promise;
    },

    /*
     * 下拉用户地址
     * */
    selectAddress:function(){
        var promise = new AV.Promise();

        query = new AV.Query(AddressFlow);
        query.notEqualTo('syncStatus',5);
        query.descending("updatedAt");
        query.limit(1000);
        query.find().then(function(obj){
            if(obj.length == 0){
                promise.resolve();
            }else{
                async.eachSeries(obj,function(each,cb){
                    each.set('syncStatus',5);
                    each.save().then(function(res){

                        var address = (res.get('address')||'');
                        var addressDetail = (res.get('addressDetail')||'');
                        var isDefault = (res.get('isDefault')||'0');
                        var phone = (res.get('phone')||'');
                        var uid = res.get('uid');
                        var name = (res.get('name')||'');

                        var res1 = new AE.Object('gr_user_addr');
                        res1.set({uid:uid,name:name,phone:phone,isDefault:isDefault,address:address,addressDetail:addressDetail});
                        res1.create().then(function(data){
                            cb(null)
                        }).catch(function(err){
                            cb(null)
                        });
                    }).catch(function(err){
                        cb(null)
                    });
                },function(){
                    promise.resolve(obj);
                })
            }
        }).catch(function(err,result){
            if(err){
                promise.reject(err);
            }else{
                promise.resolve(result);
            }
        });
        return promise;
    },

    /*
     * 下拉优惠券
     * */
    selectCoupon:function(){
        var promise = new AV.Promise();

        query = new AV.Query(CouponFlow);
        query.notEqualTo('syncStatus',5);
        query.descending("updatedAt");
        query.limit(1000);
        query.find().then(function(obj){
            if(obj.length == 0){
                promise.resolve();
            }else{
                async.eachSeries(obj,function(each,cb){
                    each.set('syncStatus',5);
                    each.save().then(function(res){

                        var uid = res.get('uid');
                        var cid = res.get('cid');
                        var used = res.get('used');
                        var coupon = ''+JSON.stringify(res.get('coupon'))+'';
                        var validityStartPeriod = (res.get('validityStartPeriod')||'0');
                        var GiveTime = ''+res.get('GiveTime')+'';
                        var validityEndPeriod = (res.get('validityEndPeriod')||'');

                        var res1 = new AE.Object('gr_user_coupon');
                        res1.set({uid:uid,cid:cid,used:used, coupon:coupon,validityStartPeriod:validityStartPeriod,GiveTime:GiveTime,validityEndPeriod:validityEndPeriod});
                        res1.create().then(function(data){
                            cb(null)
                        }).catch(function(err){
                            cb(null)
                        });
                    }).catch(function(err){
                        cb(null)
                    });
                },function(){
                    promise.resolve(obj);
                })
            }
        }).catch(function(err,result){
            if(err){
                promise.reject(err);
            }else{
                promise.resolve(result);
            }
        });
        return promise;
    }
};


